<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <style>
        ul {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        h2 {
            margin: 0;
        }

        #wrap {
            margin: 30px auto;
            width: 482px;
            padding: 5px;
            position: relative;
            border: 1px solid #000;
            background: #eee;
        }

        .title {
            font: bold 18px/40px "宋体";
            text-align: center;
            border-bottom: 1px solid #000;
        }

        #list {
            padding-left: 2px;
        }

        #list li {
            display: flex;
            opacity: 0.8;
            font: 14px/36px "宋体";
            border-bottom: 1px solid #000;
        }

        #list li a {
            padding: 0 20px;
        }

        #list li:hover {
            opacity: 1;
        }

        #list label input {
            margin: 0 20px 0 30px;
        }

        input[type="chekbox"] {
            width: 20px;
            height: 20px;
        }

        .footer {
            font: 16px/36px "宋体";
        }

       
    </style>
</head>

<body>
    <section id="wrap">
        <h2 class="title">百度音乐榜单</h2>
        <ul id="list">
            <li>
                <input type="checkbox" checked="false" />
                <span>第一条信息</span>
                <a href="javascript:;" class="collect tagger">收藏</a>
                <a href="javascript:;" class="cancelCollect">取消收藏</a>
                <a href="javascript:;" class="remove">删除</a>
            </li>
        </ul>
        <footer class="footer">
            <label><input type="checkbox" id="checkAll" />全选/全不选</label>
            <a href="javascript:;" id="remove">删除</a>
            <input type="text" id="newInfo" />
            <a href="javascript:;" id="add">添加</a>
        </footer>
    </section>
    <script>
        var data = [
            {
                id: 0,
                title: "残酷月光 - 费启鸣",
                checked: false,
                collect: true
            }, {
                id: 1,
                title: "兄弟 - 艾热",
                checked: false,
                collect: false
            }, {
                id: 2,
                title: "毕生 - 夏增祥",
                checked: false,
                collect: true
            }, {
                id: 3,
                title: "公子向北去 - 李春花",
                checked: true,
                collect: false
            }, {
                id: 4,
                title: "战场 - 沙漠五子",
                checked: false,
                collect: false //是否收藏 true 收藏 false 没有收藏
            }
        ];

        // -----------------------------------------------------------------------------------
        // 修改数据
        data.forEach(item => {
            item.checked = false;
            item.collect = false
        });

        // 根据数据渲染视图
        function renderDom(data) {
            // 获取dom元素
            var listEl = document.querySelector('#list');
            listEl.innerHTML = '';
            // 根据数据循环创建元素添加到dom视图
            data.forEach((item, index) => {
                var liEl = document.createElement('li');
                // liEl.index = index;
                liEl.innerHTML = `
                    <input type="checkbox"  ${item.checked ? 'checked' : ''} />
                    <span>${item.title}</span>
                    <a href="javascript:;" class="collect">${item.collect? '收藏':'取消收藏'}</a>
                `
                var removEl = document.createElement('a');
                removEl.innerHTML = `<a href="javascript:;" class="remove">删除</a>`;
                liEl.appendChild(removEl);
                listEl.appendChild(liEl);

                  // 点击删除按钮删除对应行
                removEl.onclick = function(){
                    data.splice(index,1);
                    console.log(data);
                    renderDom(data);
                }
            });

            // 点击收藏和取消收藏切换
            let collects = document.querySelectorAll('.collect');
            collects.forEach((item,index) => {
                item.onclick = function(){
                    data[index].collect = !data[index].collect;
                    renderDom(data);
                }
            })
            
            // 点击侧边input框 改变数据渲染视图
            var inputEl = document.querySelectorAll('li input');
            inputEl.forEach((item,index) => {
                item.addEventListener('click',function(){
                    let flag = item.checked;
                    data[index].checked = flag;
                    renderDom(data);
                })
            })

            // 渲染底部全选全不选数据
            let checkAll = document.querySelector('#checkAll');
            var num = 0;
            data.forEach(item => {
                num += item.checked;
            });
            if(num === data.length && data.length !== 0){
                checkAll.checked = true;
            } else  {
                checkAll.checked = false;
            }
        }
        renderDom(data);

        

       

        // 点击全选 全部勾选或取消
        let checkAll = document.querySelector('#checkAll');
        checkAll.addEventListener('click',e => {
            data.forEach(item =>{
                let flag = checkAll.checked;
                item.checked = flag;
            });
            renderDom(data);
        });

        // 点击底部的删除按钮 删除所有选中的数据
        let btRemove = document.querySelector('footer #remove');
        btRemove.onclick = function(){
           data = data.filter(item =>{
                return item.checked !== true;
           })
           console.log(data);
           renderDom(data);
        }

        // 点击按钮或按下Enter键添加数据
        let newInfo = document.querySelector('#newInfo');
        newInfo.addEventListener('keyup', e => {
            let keyNum = e.keyCode;
            if (keyNum === 13) {
                let newVal = newInfo.value;
                if (!newVal.trim()) {
                    alert('请输入内容');
                    return;
                }
                data.unshift({
                    title: newVal,
                    checked: false,
                    collect: false
                })
                newInfo.value = '';
            }
            renderDom(data);
        })

        let addEl = document.querySelector('#add');
        addEl.addEventListener('click', () => {
            let newVal = newInfo.value;
            if (!newVal.trim()) {
                alert('请输入内容');
                return;
            }
            data.unshift({
                title: newVal,
                checked: false,
                collect: false
            })
            newInfo.value = '';
            renderDom(data);
        })


           


    </script>
</body>

</html>